
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:qyou_app/common/base.dart';
import 'package:qyou_app/page/portal/shop/shop_carousel.dart';
import 'package:qyou_app/page/portal/shop/shop_goods.dart';
import 'package:qyou_app/page/portal/shop/shop_hot.dart';
import 'package:qyou_app/page/portal/shop/shop_store.dart';
import 'package:qyou_app/widget/refresh.dart';
import 'package:qyou_app/widget/search.dart';
import 'package:qyou_app/widget/setting.dart';

class ShopPage extends StatefulWidget {
  final _scrollController;
  ShopPage(this._scrollController);

  @override
  State<StatefulWidget> createState() => _ShopPageState(_scrollController);
}

class _ShopPageState extends State<ShopPage>
    with Base, AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin {
  //上拉刷新，下拉加载控制
  RefreshController _refreshController =
      RefreshController(initialRefresh: false);
  //滚动控制器
  final _scrollController;
  _ShopPageState(this._scrollController);

  @override
  bool get wantKeepAlive => true;

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }

  //下拉刷新，设置视频加载第一页，将获取的视频数据通知出去
  void _onRefresh() async {
    //1-删除缓存数据
    //2-获取视频数据
    //设置第一页
    //BlocProvider.of<VideoPageNoCubit>(context).reset();
    //List videoList = await Api.getVideoListData(context, 1);
    //BlocProvider.of<VideoCubit>(context).updateVideoData(videoList);
    Fluttertoast.showToast(msg: "正在刷新商城！！！");
    _refreshController.refreshCompleted();
  }

  //上拉加载
  void _onLoading() async {
    //获取之前加载的数据
    //List videoData = BlocProvider.of<VideoCubit>(context).state;
    //List videoList = await Api.getVideoListData(context);
    //videoData.addAll(videoList);
    //BlocProvider.of<VideoCubit>(context).updateVideoData(videoData);
    Fluttertoast.showToast(msg: "正在加载更多商品！！！");
    _refreshController.loadComplete();
  }

//  List category = [
//    "汉服",
//    "国潮",
//    "雕刻",
//    "国画",
//    "玉簪",
//    "丝绸",
//    "琵琶",
//    "古筝",
//    "文房四宝",
//    "手工艺品"
//  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DefaultTabController(
        length: 1,
        child: NestedScrollView(
          controller: _scrollController,
          headerSliverBuilder: (context, innerScrolled) => <Widget>[
            SliverAppBar(
              backgroundColor: Colors.red, //背景颜色
              brightness: Brightness.dark, //状态栏主题
              pinned: true, //标题栏是否固定
              floating: true, //滑动时是否悬浮
              snap: true, //当手指放开时，SliverAppBar是否会根据当前的位置展开/收起
              expandedHeight: dp(55) + dp(65), //合并高度
              actions: <Widget>[],
              flexibleSpace: FlexibleSpaceBar(
                // 下拉渐入背景
                background: Container(
                    decoration: BoxDecoration(
                      gradient: LinearGradient(
                        begin: FractionalOffset.centerLeft,
                        end: FractionalOffset.centerRight,
                        colors: <Color>[Color(0xFFd75c2e), Colors.red],
                      ),
                    ),
                    child: Row(
                      children: <Widget>[
                        Padding(
                            padding: EdgeInsets.symmetric(horizontal: dp(5))),
                        Container(
                          alignment: Alignment.center,
                          child: Image.asset("assets/shop/logo.png"),
                        ),
                        Padding(
                            padding: EdgeInsets.symmetric(horizontal: dp(5))),
                        Container(
                          alignment: Alignment.centerRight,
                          child: Text(
                            "· 长安十二坊",
                            style: TextStyle(
                                fontSize: dp(18),
                                color: Colors.white,
                                fontWeight: FontWeight.bold),
                          ),
                        ),
                        Padding(padding: EdgeInsets.only(left: dp(100))),
                      ],
                    )),
              ),
              bottom: TabBar(
                indicator: BoxDecoration(),
                isScrollable: true,
                tabs: <Widget>[
                  Row(
                    children: <Widget>[
                      Container(
                        height: dp(35),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.all(
                            Radius.circular(dp(35 / 3)),
                          ),
                        ),
                        child: Row(
                          children: <Widget>[
                            // 搜索ICON
                            Image.asset(
                              'assets/header/search.webp',
                              width: dp(25),
                              height: dp(15),
                            ),
                            SizedBox(
                              width: dp(200),
                              child: TextField(
                                //controller: _search,
                                enabled: false,
                                cursorColor: Base.defaultColor,
                                cursorWidth: 1.5,
                                style: TextStyle(
                                  color: Base.defaultColor,
                                  fontSize: 14.0,
                                ),
                                decoration: InputDecoration(
                                    border: OutlineInputBorder(
                                        borderSide: BorderSide.none),
                                    contentPadding: EdgeInsets.all(0),
                                    hintText: '搜素你喜欢的商品吧！',
                                    hintStyle: TextStyle(color: Color(0xFFBDBDBD))),
                              ),
                            ),
                            Image.asset(
                              'assets/header/photo.png',
                              width: dp(25),
                              height: dp(15),
                            ),
                          ],
                        ),
                      ),
                      Padding(
                          padding: EdgeInsets.symmetric(horizontal: dp(5))),
                      Image.asset(
                        "assets/shop/category.png",
                        width: dp(25),
                      ),
                      Padding(
                          padding: EdgeInsets.symmetric(horizontal: dp(5))),
                      Image.asset(
                        "assets/shop/shop-car.png",
                        width: dp(25),
                      ),
                      Padding(
                          padding: EdgeInsets.symmetric(horizontal: dp(5))),
                      Image.asset(
                        "assets/shop/profile.png",
                        width: dp(25),
                      ),
                    ],
                  ),
                ],
              ),
              forceElevated: innerScrolled, //是否显示阴影
            ),
          ],
          body: TabBarView(
            physics: NeverScrollableScrollPhysics(),
            children: [Container(
              //color: Colors.green,
              child: ScrollConfiguration(
                behavior: BehaviorNull(),
                child: RefreshConfiguration(
                  headerTriggerDistance: dp(80),
                  maxOverScrollExtent : dp(100),
                  footerTriggerDistance: dp(50),
                  maxUnderScrollExtent: 0,
                  headerBuilder: () => MaterialClassicHeader(),
                  footerBuilder: () => ClassicFooter(),
                  child: SmartRefresher(
                    enablePullDown: true,
                    enablePullUp: true,
                    footer: RefreshFooter(bgColor: Color(0xfff1f5f6),),
                    controller: _refreshController,
                    onRefresh: _onRefresh,
                    onLoading: _onLoading,
                    child: CustomScrollView(
                      // physics: BouncingScrollPhysics(),
                      slivers: <Widget>[
                        SliverToBoxAdapter(
                          child: Container(
                            child: Column(
                              children: [
                                ShopCarouselPage(),
                                //ShopStorePage(),
                                ShopHotPage(),
                                ShopGoodsPage(_scrollController),
                                //CarouselPage(),
                                //VideoPage(),
                                //CateList(),
                                //BroadcastSwiper(),
                                //LiveList(),
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ),
            ),],
          ),
        ),
      ),
    );
  }
}
